<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="100px"
      v-loading="formLoading"
    >
       <el-form-item label="阶段号" prop="jdh">
        <el-input v-model="formData.jdh" placeholder="请输入阶段号" />
      </el-form-item>
      <el-form-item label="收付款类型" prop="sfklx">
        <el-select v-model="formData.sfklx" placeholder="请选择收付款类型">
          <el-option
            v-for="dict in getStrDictOptions(DICT_TYPE.CONT_SFKFS)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="收付款方向" prop="sfkfx">
        <el-select v-model="formData.sfkfx" placeholder="请选择收付款方向">
          <el-option
            v-for="dict in getStrDictOptions(DICT_TYPE.CONT_SFKFX)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="应收付金额" prop="ysfje">
        <el-input v-model="formData.ysfje" placeholder="请输入应收付金额" />
      </el-form-item>
      <el-form-item label="实际应收付金额" prop="sjysfje">
        <el-input v-model="formData.sjysfje" placeholder="请输入实际应收付金额" />
      </el-form-item>
      <el-form-item label="实际收付金额" prop="sjsfje">
        <el-input v-model="formData.sjsfje" placeholder="请输入实际收付金额" />
      </el-form-item>
      <el-form-item label="未收付金额" prop="wsfje">
        <el-input v-model="formData.wsfje" placeholder="请输入未收付金额" />
      </el-form-item>
      <el-form-item label="应收付日期" prop="ysfrq">
        <el-date-picker
          v-model="formData.ysfrq"
          type="date"
          value-format="x"
          placeholder="选择应收付日期"
        />
      </el-form-item>
      <el-form-item label="实际应收付日期" prop="sjysfrq">
        <el-date-picker
          v-model="formData.sjysfrq"
          type="date"
          value-format="x"
          placeholder="选择实际应收付日期"
        />
      </el-form-item>
      <el-form-item label="差额原因" prop="ceyy">
        <el-input v-model="formData.ceyy" placeholder="请输入差额原因" />
      </el-form-item>
      <el-form-item label="支付方式" prop="zffs">
        <el-input v-model="formData.zffs" placeholder="请输入支付方式" />
      </el-form-item>
      <el-form-item label="收付款单据号" prop="djh">
        <el-input v-model="formData.djh" placeholder="请输入收付款单据号" />
      </el-form-item>
      <el-form-item label="结算时间" prop="jssj">
        <el-date-picker
          v-model="formData.jssj"
          type="date"
          value-format="x"
          placeholder="选择结算时间"
        />
      </el-form-item>
      <el-form-item label="结算单据号" prop="jsdjh">
        <el-input v-model="formData.jsdjh" placeholder="请输入结算单据号" />
      </el-form-item>
      <el-form-item label="是否开具发票" prop="sfkjfp">
        <el-select v-model="formData.sfkjfp" placeholder="请选择是否开具发票">
          <el-option
            v-for="dict in getBoolDictOptions(DICT_TYPE.INFRA_BOOLEAN_STRING)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="发票号" prop="fph">
        <el-input v-model="formData.fph" placeholder="请输入发票号" />
      </el-form-item>
      <el-form-item label="开票单位" prop="khfzr">
        <el-input v-model="formData.khfzr" placeholder="请输入开票单位" />
      </el-form-item>
      <el-form-item label="发票金额" prop="fpjr">
        <el-input v-model="formData.fpjr" placeholder="请输入发票金额" />
      </el-form-item>
      <el-form-item label="经手人" prop="jsr">
        <el-input v-model="formData.jsr" placeholder="请输入经手人" />
      </el-form-item>
      <el-form-item label="票据运送方式" prop="pjysfs">
        <el-input v-model="formData.pjysfs" placeholder="请输入票据运送方式" />
      </el-form-item>
      <el-form-item label="票据运输号" prop="pjysh">
        <el-input v-model="formData.pjysh" placeholder="请输入票据运输号" />
      </el-form-item>
      <el-form-item label="备注" prop="bz">
        <el-input v-model="formData.bz" placeholder="请输入备注" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import { getStrDictOptions, getBoolDictOptions, DICT_TYPE } from '@/utils/dict'
import * as BaseInfoApi from '@/api/cont/base'

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  id: undefined,
  htid: undefined,
  jdh: undefined,
  sfklx: undefined,
  sfkfx: undefined,
  ysfje: undefined,
  sjysfje: undefined,
  sjsfje: undefined,
  wsfje: undefined,
  ysfrq: undefined,
  sjysfrq: undefined,
  ceyy: undefined,
  zffs: undefined,
  djh: undefined,
  jssj: undefined,
  jsdjh: undefined,
  sfkjfp: undefined,
  fph: undefined,
  khfzr: undefined,
  fpjr: undefined,
  jsr: undefined,
  pjysfs: undefined,
  pjysh: undefined,
  bz: undefined
})
const formRules = reactive({
  htid: [{ required: true, message: '合同编号不能为空', trigger: 'blur' }],
  jdh: [{ required: true, message: '阶段号不能为空', trigger: 'blur' }]
})
const formRef = ref() // 表单 Ref

/** 打开弹窗 */
const open = async (type: string, id?: number, htid: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()
  formData.value.htid = htid
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await BaseInfoApi.getPayInfo(id)
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value
    if (formType.value === 'create') {
      await BaseInfoApi.createPayInfo(data)
      message.success(t('common.createSuccess'))
    } else {
      await BaseInfoApi.updatePayInfo(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    htid: undefined,
    jdh: undefined,
    sfklx: undefined,
    sfkfx: undefined,
    ysfje: undefined,
    sjysfje: undefined,
    sjsfje: undefined,
    wsfje: undefined,
    ysfrq: undefined,
    sjysfrq: undefined,
    ceyy: undefined,
    zffs: undefined,
    djh: undefined,
    jssj: undefined,
    jsdjh: undefined,
    sfkjfp: undefined,
    fph: undefined,
    khfzr: undefined,
    fpjr: undefined,
    jsr: undefined,
    pjysfs: undefined,
    pjysh: undefined,
    bz: undefined
  }
  formRef.value?.resetFields()
}
</script>